package com.tieba.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

@SuppressWarnings("deprecation")
public class HttpConnection {

	private static HttpClient httpClient = new DefaultHttpClient();

	public static Map<String, Object> doPost(String url, Map<String, String> paramMap) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {

			List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
			for (String key : paramMap.keySet()) {
				nameValuePairList.add(new BasicNameValuePair(key, paramMap.get(key)));
			}

			UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8");

			HttpPost httpPost = new HttpPost(url);
			httpPost.setEntity(urlEncodedFormEntity);

			httpPost.setHeader("accept", "*/*");
			httpPost.setHeader("connection", "Keep-Alive");
			httpPost.setHeader("user-agent", Constants.useragent);
			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

			HttpResponse httpResponse = httpClient.execute(httpPost);

			Header[] headers = httpResponse.getAllHeaders();
			result.put("headers", headers);
			result.put("content", EntityUtils.toString(httpResponse.getEntity(), "utf-8"));

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static String doGet(String url, Map<String, String> paramMap) {
		String result = null;
		try {

			String urlNameString = url + "?";
			for (String key : paramMap.keySet()) {
				urlNameString = urlNameString + key + "=" + paramMap.get(key) + "&";
			}
			urlNameString = urlNameString.substring(0, urlNameString.length() - 1);
			HttpGet get = new HttpGet(url);

			get.setHeader("accept", "*/*");
			get.setHeader("connection", "Keep-Alive");
			get.setHeader("user-agent", Constants.useragent);
			get.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

			HttpResponse httpResponse = httpClient.execute(get);

			result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static void endHttpClient() {
		httpClient.getConnectionManager().shutdown();
		httpClient = null;
		httpClient = new DefaultHttpClient();
	}

}
